import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_plugin_demo2/flutter_plugin_demo2.dart';
import 'package:flutter_plugin_demo2_example/more_Params_page.dart';
import 'package:flutter_plugin_demo2_example/show_native_page.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      routes: {
        "/moreParams": (context) => MoreParamsPage(),
      },
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Plugin example app'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('Running on: $_platformVersion\n'),
            const Divider(),
            Text(_originalStr + "\n"),
            InkWell(
              onTap: () {

                print("flutter调用原生");
                updateAndroidInfoData();
              },
              child: Container(
                padding: const EdgeInsets.all(8),
                color: Colors.red,
                child: Text("flutter调用原生~~"),
              ),
            ),
            const Divider(),
            InkWell(
                onTap: () async {
                  print("flutter传值");

                  // Navigator.push(
                  //   context,
                  //   MaterialPageRoute(builder: (context) => MoreParamsPage()),
                  // );
                  Navigator.pushNamed(context, "/moreParams");
                },
                child: Container(
                  padding: const EdgeInsets.all(8),
                  color: Colors.red,
                  child: Text("flutter传值~~"),
                )),
            const Divider(),
            InkWell(
                onTap: () async {
                  print("flutter显示原生View");

                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => ShowNativePage()),
                  );
                },
                child: Container(
                  padding: const EdgeInsets.all(8),
                  color: Colors.red,
                  child: Text("flutter显示原生View~~"),
                )),
          ],
        ),
      ),
    );
  }

  String _platformVersion = 'Unknown';
  String _originalStr = "init data";

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await FlutterPluginDemo2.platformVersion ??
          'Unknown platform version';

    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  Future<void> updateAndroidInfoData() async {
    String originalStr;

    try {
      originalStr =
          await FlutterPluginDemo2.originalAllData ?? "Unknow Not Back";
    } on PlatformException {
      originalStr = "Failed Not Back";
    }
    if (!mounted) return;

    setState(() {
      _originalStr = originalStr;
    });
  }
}
